$(document).ready(function () {
    init();
    $(".xuanzhepage").change(function() {
        findByName(1);
    });
});


function deleteBatch() {
    var data = $("input[type='checkbox'][name='supplierCheck']:checked");
    if (data.length == 0) {
        alert("请选择您要删除的数据。")
        return;
    }
    var ids = [];

    data.each(function (){
        ids.push($(this).val())
    });

    if (ids.length == 0){
        alert('未获取到要删除的数据。')
        return;
    }


    $.ajax({
        url: "/user/deleteBatch",
        type: 'POST',
        data: {
            "ids": ids
        },
        dataType: "JSON",
        traditional:true,
        async: false,
        success: function (res) {
            if(res.code == "200") {
                alert("删除成功");
                init();
            } else {
                alert(res.msg);
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })

}

function fileExport() {
    var username = $("input[name='username']").val();
    var trueName = $("input[name='trueName']").val();
    window.location.href = "/user/fileExport?username=" + username + "&trueName=" + trueName;


}

function init() {

    $.ajax({
        url: "/user/find",
        type: 'GET',
        data: {},
        dataType: "JSON",
        async: false,
        success: function (res) {

            if (res.code === "200") {
                var data = res.data.list;
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    html += "<tr>";
                    html += "<td><input type='checkbox' name='supplierCheck' value='" + data[i].id + "'></td>";
                    html += "<th>" + data[i].username + "</th>";
                    html += "<th>" + data[i].password + "</th>";
                    html += "<th>" + data[i].trueName + "</th>";
                    html += "<th>" + data[i].phone + "</th>";
                    html += "<th>" + (data[i].userLock == 0 ? "启动" : "禁用") + "</th>";
                    html += "<th>" + data[i].createTime + "</th>";
                    html += "<th>";

                    if (data[i].userLock == 0) {
                        html += "<button class='btn btn-primary' onclick='disable(" + data[i].id + ")'>禁用用户</buttonclass> ";
                    } else {
                        html += "<button class='btn btn-primary' onclick='enable(" + data[i].id + ")'>启用用户</buttonclass> ";
                    }

                    html += "<button class='btn btn-primary' onclick='deleteFenlei(" + data[i].id + ")'>删除用户</buttonclass> ";
                    html += "</th>";
                    html += "</tr>";
                }

                $(".tbody").html(html);

                // 尾页
                var totalPage = res.data.navigateLastPage;

                // 上一页
                var upPage = res.data.prePage;
                if (res.data.isFirstPage) {
                    upPage = 1;
                }

                // 下一页
                var lowerPage = res.data.nextPage;
                if (res.data.isLastPage) {
                    lowerPage = res.data.navigateLastPage;
                }

                $(".firstPage").attr("onClick", "findByName(1)");
                $(".upPage").attr("onClick", "findByName(" + upPage + ")");
                $(".lowerPage").attr("onClick", "findByName(" + lowerPage + ")");
                $(".totalPage").attr("onClick", "findByName(" + totalPage + ")");

                $(".tbodyInfo").html(html);

            } else {
                alert(res.msg)
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })

}

function disable(id) {
    $.ajax({
        url: "/user/disable/" + id,
        type: 'GET',
        data: {},
        dataType: "JSON",
        async: false,
        success: function (res) {
            if (res.code === "200") {
                init();
            } else {
                alert(res.msg)
                init();
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })

}

function enable(id) {
    $.ajax({
        url: "/user/enable/" + id,
        type: 'GET',
        data: {},
        dataType: "JSON",
        async: false,
        success: function (res) {
            if (res.code === "200") {
                init();
            } else {
                alert(res.msg)
                init();
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })

}

function findByName(currentPage) {
    var xuanzhepage = $(".xuanzhepage").val();
    var username = $("input[name='username']").val();
    var trueName = $("input[name='trueName']").val();

    $.ajax({
        url: "/user/findByName",
        type: 'GET',
        data: {
            'username': username,
            'trueName': trueName,
            'xuanzhepage': xuanzhepage,
            'currentPage': currentPage
        },
        dataType: "JSON",
        async: false,
        success: function (res) {
            if (res.code === "200") {
                var data = res.data.list;
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    html += "<tr>";
                    html += "<td><input type='checkbox' name='supplierCheck' value='" + data[i].id + "'></td>";
                    html += "<th>" + data[i].username + "</th>";
                    html += "<th>" + data[i].password + "</th>";
                    html += "<th>" + data[i].trueName + "</th>";
                    html += "<th>" + data[i].phone + "</th>";
                    html += "<th>" + data[i].createTime + "</th>";
                    html += "<th>" + (data[i].userLock == 0 ? "启动" : "禁用") + "</th>";
                    html += "<th>";

                    if (data[i].userLock == 0) {
                        html += "<button class='btn btn-primary' onclick='disable(" + data[i].id + ")'>禁用用户</buttonclass> ";
                    } else {
                        html += "<button class='btn btn-primary' onclick='enable(" + data[i].id + ")'>启用用户</buttonclass> ";
                    }

                    html += "<button class='btn btn-primary' onclick='deleteFenlei(" + data[i].id + ")'>删除用户</buttonclass> ";
                    html += "</th>";
                    html += "</tr>";
                }
                $(".tbody").html(html);

                // 尾页
                var totalPage = res.data.navigateLastPage;

                // 上一页
                var upPage = res.data.prePage;
                if (res.data.isFirstPage) {
                    upPage = 1;
                }

                // 下一页
                var lowerPage = res.data.nextPage;
                if (res.data.isLastPage) {
                    lowerPage = res.data.navigateLastPage;
                }

                $(".firstPage").attr("onClick", "findByName(1)");
                $(".upPage").attr("onClick", "findByName(" + upPage + ")");
                $(".lowerPage").attr("onClick", "findByName(" + lowerPage + ")");
                $(".totalPage").attr("onClick", "findByName(" + totalPage + ")");

                $(".tbodyInfo").html(html);

            } else {
                alert(res.msg)
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })

}

function deleteFenlei(id) {

    if (!confirm("确定要删除该用户吗")) {
        return false;
    }

    $.ajax({
        url: "/user/userDelete/" + id,
        type: 'DELETE',
        data: {},
        dataType: "JSON",
        async: false,
        success: function (res) {
            if (res.code === "200") {
                init();
            } else {
                alert(res.msg)
            }
        },
        error: function (e) {
            alert('发生错误')
        }

    })
}
